﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KickInWinForm
{
    internal class KickForQiPassword
    {
        #region 暴露数据
        /// <summary>
        /// 这个是最终密码，以数组形式保留。
        /// </summary>
        public List<int> PasswordList = new List<int>();

        /// <summary>
        /// 密码长度，这个长度决定了上面密码数组的长度。
        /// </summary>
        public int PasswordLength = 4;

        /// <summary>
        /// 单位计时周期，在这个时间内点击都被当做连续操作。
        /// 超过这个周期，被认定中断了连续点击操作。
        /// </summary>
        public int TimerInterval = 1000;

        /// <summary>
        /// 密码字符串
        /// </summary>
        public string PasswordString => GetPassword();

        
        #endregion


        #region 内部数据，不暴露
        /// <summary>
        /// 单一事件周期内点击的次数
        /// 下一个周期，要清零。
        /// </summary>
        private int clickCount_In_Time { get; set; } = 0;

        /// <summary>
        /// 连续点击的次数，用来记录某个单次密码。
        /// 一般使用这个变量加上周期内点击次数来做统计。
        /// 注意：下一个独立的统计周期，这个变量要清零。
        /// </summary>
        private int clickCount_Total_Continuous { get; set; } = 0;

        /// <summary>
        /// 计时器
        /// </summary>
        private System.Timers.Timer timer;
        #endregion

        public delegate void ClickFuction();//页面数据刷新
        public event ClickFuction Reflash_Out_Page;

        /// <summary>
        /// 构造函数
        /// </summary>
        public KickForQiPassword()
        {
            timer = new System.Timers.Timer();
            timer.Elapsed += Timer_Elapsed;
            SetTimer(TimerAction.Stop);
        }

        #region 可被外部调用方法
        /// <summary>
        /// 点击一下
        /// </summary>
        public void Kick()
        {
            if (!timer.Enabled)
            {
                clickCount_Total_Continuous = 0;
                clickCount_In_Time = 0;
                SetTimer(TimerAction.Run);
            }
            clickCount_In_Time = clickCount_In_Time + 1;

        }
        public void TestResult()
        {
            MessageBox.Show(PasswordString);
        }
        /// <summary>
        /// 清空密码数组
        /// </summary>
        public void ClearPassword()
        {
            PasswordList.Clear();
        }

        private void Reflash_Page()
        {
            PasswordList.Add(clickCount_Total_Continuous);

            while (PasswordList.Count > PasswordLength)
            {
                PasswordList.RemoveAt(0);
            }
            Reflash_Out_Page?.Invoke();
        }

        public bool IsEqual(string value)
        {
            bool result = true; //默认进来是相符的。
            if (PasswordString.Length > 0 && PasswordString.Length >= value.Length)
            {
                var pL = PasswordString.Length - 1;
                var vL = value.Length - 1;

                for (int i = 0; i < value.Length; i++)
                {
                    //仅仅对位最后几个数字
                    if (PasswordString[pL - i] == value[vL - i])
                    {
                        //继续保持相符
                    }
                    else
                    {
                        result = false; //只要有一点不同，就是不同。
                        break;
                    }
                }
                
            }
            else
            {
                result = false;
            }

            return result;
        }
        #endregion




        #region 内部方法
        /// <summary>
        /// 计时器执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
        {
            if (clickCount_In_Time != 0)//单位时间内有点击，咱们继续记录
            {
                clickCount_Total_Continuous = clickCount_Total_Continuous + clickCount_In_Time;
                clickCount_In_Time = 0;
            }
            else
            {
                SetTimer(TimerAction.Stop);
                Reflash_Page();
            }
        }
        /// <summary>
        /// 密码获取方式
        /// </summary>
        /// <returns></returns>
        private string GetPassword()
        {
            string result = "";

            for (int i = 0; i < PasswordList.Count; i++)
            {
                if (i == 0)
                {
                    result = $"{PasswordList[i]}";
                }
                else
                {
                    result = $"{result}{PasswordList[i]}";
                }
            }
            return result;
        }
        /// <summary>
        /// 设定计时器
        /// </summary>
        /// <param name="status">0 停止，1启动</param>
        private void SetTimer(TimerAction status)
        {
            switch (status)
            {
                case TimerAction.Stop:
                    {
                        timer.Enabled = false;
                        timer.Interval = TimerInterval;
                        timer.Stop();
                    }
                    break;
                case TimerAction.Run:
                    {
                        timer.Enabled = true;
                        timer.Interval = TimerInterval;
                        timer.Start();
                    }
                    break;
                default:
                    break;
            }
        }
        #endregion
    }


    /// <summary>
    /// 计时器动作
    /// </summary>
    internal enum TimerAction
    {
        /// <summary>
        /// 开始计时
        /// </summary>
        Run,
        /// <summary>
        /// 停止计时
        /// </summary>
        Stop
    }
}
